package com.xiaozhen.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaozhen.security.controller.BaseController;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.system.SysDictType;
import com.xiaozhen.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/8/5 9:40
 * @description:
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/sysDict/type")
public class SysDictTypeController extends BaseController {

    @Autowired
    private SysDictTypeService dictTypeService;


    @ApiOperation("字典类型列表")
    @GetMapping("/list")
    public AjaxResult list(SysDictType dictType) {
        Page<SysDictType> page = startPage();
        Page<SysDictType> list = dictTypeService.selectDictTypeList(page, dictType);
        return getDataTable(list);
    }

    @ApiOperation("新增字典类型")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictType dictType) {
        if (!dictTypeService.checkDictTypeUnique(dictType)) {
            return AjaxResult.error("新增字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        dictType.setCreateBy(getCurrentUserId());
        dictType.setCreateTime(LocalDateTime.now());
        return toAjax(dictTypeService.insertDictType(dictType));
    }

    @ApiOperation("修改字典类型")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody SysDictType dictType) {
        if (!dictTypeService.checkDictTypeUnique(dictType)) {
            return AjaxResult.error("修改字典'" + dictType.getDictName() + "'失败，字典类型已存在");
        }
        dictType.setUpdateBy(getCurrentUserId());
        dictType.setUpdateTime(LocalDateTime.now());
        return toAjax(dictTypeService.updateDictType(dictType));
    }

    @ApiOperation("删除字典类型")
    @DeleteMapping("/{dictId}")
    public AjaxResult remove(@PathVariable Long dictId) {
        return toAjax(dictTypeService.deleteDictTypeByIds(dictId));
    }
}
